<?php
if(!defined('DCS_ROOT')) exit('Access Denied');

class main {

	function __construct() {		
		require_once(APP_PATH."model/accounts/init.php");
		$action = $url['action'];
		if($action == 'main'){
			$this->defaultAction();
		}elseif($action == 'avatars'){
			$this->avatars();
		}elseif($action == 'password'){
			$this->password();
		}else{
			pageError();
		}
		exit();
	}

	function defaultAction(){  //这里实际是查用户profile数据
		$url = getUrl();
		$db = getDB();
		require_once(APP_PATH."model/accounts/user.php" );
		$data = new UserModel($db);
		$uid = $this->uid;
		$userInfo = $data->getUserbasic($uid);
		if(!isPost()){
			$tpl = new Template('accounts/main.htm');
			$userInfo['regtime'] = date("Y-m-d G:i:H",$userInfo['regtime']);
			$userInfo['lasttime'] = date("Y-m-d G:i:H",$userInfo['lasttime']);
			$tpl->assign($userInfo);		
			$tpl->assign(getMember());
			$tpl->output();
		}else{
			$tel = $_POST['tel'];
			$qq = $_POST['qq'];
			$msn = $_POST['msn'];
			$address = $_POST['address'];
			$sign = $_POST['sign'];

			$isedit = $data->editUser($uid,$tel,$qq,$msn,$address,$sign);
			if(!empty($isedit)){
				message('修改成功');
			}else{
				message('修改失败');
			}
		}
	}

	function avatars(){
		$url = getUrl();
		$db = getDB();
		require_once(APP_PATH."model/accounts/user.php" );
		$data = new UserModel($db);
		$uid = $this->uid;
		$userInfo = $data->getUserbasic($uid);			
		if(!isPost()){
			$tpl = new Template('accounts/avatars.htm');
			$userInfo['avatars'] = empty($userInfo['avatars']) ? 'default.gif' : $userInfo['avatars'];
			$tpl->assign($userInfo);		
			$tpl->assign(getMember());
			$tpl->output();
		}else{
			if(!empty($_FILES['avatars']['name'])){
				require_once(CLASS_PATH."class/upload.class.php" );
				$upload = new Upload();	
				$upload->config(array(
					'targetDir' => '../avatars/b/',
					'saveType' => 0
				));
				$files = $upload->saveFiles('avatars');
				$avatars = $files['0']['filenames'];
				
				$sourFile = PUB_PATH.'avatars/b/'.$avatars;
				$targetFile = PUB_PATH.'avatars/'.$avatars;
				thumbs($sourFile,$targetFile,48,48);
			}
			if(!empty($avatars)){
				$isedit = $data->editAvatars($uid,$avatars);
				if(!empty($isedit)){
					message('修改头像成功');
				}else{
					message('修改头像失败');
				}
			}else{
				message('没有头像需要修改。');
			}
		}
	}

	function password(){
		$url = getUrl();
		$db = getDB();
		require_once(APP_PATH."model/accounts/user.php" );
		$data = new UserModel($db);
		$uid = $this->uid;
		$userInfo = $data->getUserbasic($uid);
		if(!isPost()){
			$tpl = new Template('accounts/password.htm');
			$tpl->assign($userInfo);		
			$tpl->assign(getMember());
			$tpl->output();
		}else{
			$password = md5($_POST['password']);
			$newpw = $_POST['newpassword'];
			$newpw2 = $_POST['newpassword2'];

			if($newpw != $newpw2){
				message('两次输入的密码不相同');
			}

			$isedit = $data->editPassword($uid,$password,$newpw);
			if(!empty($isedit)){
				session_start();
				$_SESSION = array();
				unset($_SESSION);
				message('修改密码成功');
			}else{
				message('修改密码失败');
			}
		}
	}
}
?>
